package com.msb.mall.thirdPart.controller.upload;

import com.msb.mall.thirdPart.constants.UploadConstants;
import com.msb.common.vo.CommonResult;
import com.msb.mall.thirdPart.dto.UploadServerReqDTO;
import com.msb.mall.thirdPart.service.UploadServiceImpl;
import com.msb.mall.thirdPart.vo.UploadFile;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 上传文件到搭建的虚拟机服务器
 *
 * @author xulei
 * @date 2024/5/17 12:17
 */
@RestController
@RequestMapping("thirdPart/server")
public class UploadServerController {

    @Autowired
    private UploadServiceImpl uploadServiceImpl;

    /**
     * 上传linux服务器
     * @return
     */
    @PostMapping("/upload")
    public CommonResult uploadServerSingle(@RequestBody UploadServerReqDTO reqDTO) {

        if(!StringUtils.isNotEmpty(reqDTO.getUploadType())) {
            return CommonResult.error(HttpStatus.SC_BAD_REQUEST, "缺失参数【uploadType】");
        }
        if(!StringUtils.isNotEmpty(reqDTO.getOperateType())) {
            return CommonResult.error(HttpStatus.SC_BAD_REQUEST, "缺失参数【operateType:0新增|1修改|2删除】");
        }
        if(UploadConstants.UPLOAD_OPR_ADD.equals(reqDTO.getOperateType()) &&
                CollectionUtils.isEmpty(reqDTO.getFileList())) {
            return CommonResult.error(HttpStatus.SC_BAD_REQUEST, "新增操作，【fileList】不能为空");
        }

        if(UploadConstants.UPLOAD_OPR_UPDATE.equals(reqDTO.getOperateType()) &&
                (CollectionUtils.isEmpty(reqDTO.getFileList()) || CollectionUtils.isEmpty(reqDTO.getDeletePaths()))) {
            return CommonResult.error(HttpStatus.SC_BAD_REQUEST, "修改操作，【fileList】【deletePaths】都不能为空");
        }

        if(UploadConstants.UPLOAD_OPR_DELETE.equals(reqDTO.getOperateType()) &&
                CollectionUtils.isEmpty(reqDTO.getDeletePaths())) {
            return CommonResult.error(HttpStatus.SC_BAD_REQUEST, "删除操作，【deletePaths】不能为空");
        }

        // 单文件上传
        if(UploadConstants.is_single_file.equals(reqDTO.getIsSingleFile())) {
            List<UploadFile> fileList = reqDTO.getFileList();
            return uploadServiceImpl.uploadServerSingle(reqDTO);
        } else {
            return uploadServiceImpl.uploadServerMulti(reqDTO);
        }
    }
}
